﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Surface.Presentation.Controls;

namespace navigation.Controls
{
    /// <summary>
    /// Interaction logic for TaskCardContent.xaml
    /// </summary>
    public partial class TaskCardContent : UserControl
    {
        public TaskCardContent()
        {
            InitializeComponent();
        }

        public static DependencyProperty TaskNameProperty = DependencyProperty.Register("TaskName", typeof(string), typeof(TaskCardContent));

        public string TaskName
        {
            get
            {
                return (string)GetValue(TaskCardContent.TaskNameProperty);
            }
            set
            {
                SetValue(TaskCardContent.TaskNameProperty, value);
            }
        }

        public static DependencyProperty TaskContentProperty = DependencyProperty.Register("TaskContent", typeof(string), typeof(TaskCardContent));

        public string TaskContent
        {
            get
            {
                return (string)GetValue(TaskCardContent.TaskContentProperty);
            }
            set
            {
                SetValue(TaskCardContent.TaskContentProperty, value);
            }
        }        

        private T GetVisualChild<T>(Visual parent) where T : Visual 
        { 
            T child = default(T); 
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
            for (int i = 0; i < numVisuals; i++) 
            { 
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
                child = v as T; 
                if (child == null)
                { 
                    child = GetVisualChild<T>(v); 
                } 
                if (child != null) 
                { 
                    break; 
                } 
            } 
            return child; 
        }

        private void TaskNameTextBox_KeyboardPositioning(object sender, KeyboardPositioningEventArgs e)
        {
            FrameworkElement findSource = sender as FrameworkElement;
            ScatterViewItem draggedElement = null;

            // Find the ScatterViewItem object that is being touched.
            while (draggedElement == null && findSource != null)
            {
                if ((draggedElement = findSource as ScatterViewItem) == null)
                {
                    findSource = VisualTreeHelper.GetParent(findSource) as FrameworkElement;
                }
            }

            float distance = (float)(draggedElement.ActualHeight / 2 + e.Height + 20);
            float theta = (float)draggedElement.Orientation;       
            float newCenterX = (float)(draggedElement.Center.X - distance * Math.Sin(theta));
            float newCenterY = (float)(draggedElement.Center.Y + distance * Math.Cos(theta));
            e.CenterX = newCenterX;
            e.CenterY = newCenterY;
            e.Rotation = Math.PI * theta / 180.0;

            //test
            //Console.WriteLine("test begin");
            //ScatterView sv = null;
            //FrameworkElement findSource2 = sender as FrameworkElement;
            //while (sv == null && findSource2 != null)
            //{
            //    if ((sv = findSource2 as ScatterView) == null)
            //    {
            //        findSource2 = VisualTreeHelper.GetParent(findSource2) as FrameworkElement;
            //    }
            //}
            //Label testItem = new Label();
            //sv.Items.Add(testItem);
            //ScatterViewItem svi = sv.ItemContainerGenerator.ContainerFromItem(testItem) as ScatterViewItem;
            //svi.Center = new Point(newCenterX, newCenterY);
            //svi.Orientation = theta;            
        } 

    }
}
